package com.cloud.sysmana.web;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.cloud.sysmana.entity.SysFunc;
import com.cloud.sysmana.ser.ISysFuncService;
import com.cloud.sysmana.ser.impl.FuncRoleCacheService;
import com.cloud.util.base.BaseAction;
import com.cloud.util.base.RetType;

/**
 * 功能Action
 * @author gaof
 * @date 2016年4月17日
 */
@Controller
@RequestMapping("system")
public class FuncAction extends BaseAction{

	@Resource(name="sysFuncService")
	private ISysFuncService sysFuncService;
	
	/* 功能、岗位数据缓存 */
	@Resource(name="funcRoleCache")
	private FuncRoleCacheService funcRoleCache;
	
	/**
	 * 获取功能树列表
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("listFunc.do")
	public String listFunc(ModelMap modelMap){
		
		String funcTreeJson = sysFuncService.listFuncTreeJson();
		modelMap.put("funcTree", funcTreeJson);
		
		return getViewsModelPathSys("sysmana/func/listFunc");
	}
	
	/**
	 * 添加功能
	 * @param modelMap
	 * @param func
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("addFunc.ido")
	public RetType addOrUpdateFunc(ModelMap modelMap, SysFunc func, HttpServletRequest request){
		return sysFuncService.addOrUpdateFunc(func);
	}
	
	/**
	 * 删除功能
	 * @param modelMap
	 * @param func
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("delFunc.ido")
	public RetType delFunc(ModelMap modelMap, SysFunc func, HttpServletRequest request){
		
		sysFuncService.delLinkFunc(func);
		
		RetType retType = new RetType();
		retType.setErrCode(RetType.RET_SUCCESS);
		
		return retType;
	}
	
	/**
	 * 导出功能
	 * @param request
	 * @param response
	 * @param exportId
	 * @return
	 */
	@RequestMapping("exportFunc.do")
	public ModelAndView exportFunc(HttpServletRequest request, HttpServletResponse response, String exportId){
		
		sysFuncService.funcOutput(request, response, exportId);
		
		return null;
	}
	
	/**
	 * 导入功能
	 * @param request
	 * @param file
	 * @param importId
	 * @return
	 */
	@RequestMapping("importFunc.do")
	public String importFunc(HttpServletRequest request, @RequestParam MultipartFile[] file, String importId){
		
		sysFuncService.funcImput(request, file, importId);
		
		return "redirect:/system/listFunc.do";
	}
	
}
